"""
处理数据，给节点加上id等信息，处理成前端数据结构，
"""
import ast
import json
from werkzeug.datastructures import ImmutableMultiDict


# 调整数据库格式
def format_data(data):
    # 调整格式
    for key in data:
        data_key = data[key]
        if isinstance((data[key]), bytes):
            data[key] = data_key.decode('utf-8')
            # 处理换行符等特殊字符
            # data[key] = data[key].replace("\\n", "\n").replace("\\t", "\t")
            # 将字符串格式化列表字典等形式

        # data[key] = json.loads(data_key)
        # json化
        try:
            data[key] = json.loads(data_key)
        except Exception as e:
            continue

    # res = json.dumps(data, ensure_ascii=False, sort_keys=True, indent=4, separators=(',', ': '))
    return data


# 调整参数格式
def format_request_params(params):

    mutable_dict = params.copy()
    for i in params:
        try:
            mutable_dict[i] = ast.literal_eval(mutable_dict[i])
        except Exception as e:
            continue
    modified_immutable_dict = ImmutableMultiDict(mutable_dict.items())

    # print(modified_immutable_dict)
    return modified_immutable_dict